﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Client.UIL.ManageAdmin;
using OpenSoftStore.Client.UIL.ManagePrinter;
using OpenSoftStore.Orm.Static;

namespace OpenSoftStore.Client.UIL
{
    /// <summary>
    /// 对象名称：应用程序主窗体（用户界面层）
    /// 对象说明：该窗体由上部的Header、左侧的导航栏及中部的模块区域（PnlContent）组成。
    ///           用户通过单击导航栏中的按钮，来切换窗体中部模块区域中的功能模块的显示。
    /// 作者姓名：
    /// 编写日期：
    /// </summary>
    public partial class FormMainSpa : Form
    {
        private static FormMainSpa instance;
        public AdminLogic adminBll;
        public int Top = 0;
        public int Left = 0;
        public Boolean Drog = false;

        #region Cache
        /// <summary>
        /// 返回该窗体的唯一实例。如果之前该窗体没有被创建，进行创建并返回该窗体的唯一实例。
        /// 此处采用单键模式对窗体的唯一实例进行控制，以便外界窗体或控件可以随时访问本窗体。
        /// </summary>
        /// 
        public static FormMainSpa Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FormMainSpa();
                }
                return instance;
            }
        }

        #endregion
        /// <summary>
        /// 私有的窗体实例化方法，创建实例只能通过主窗体的Instance属性实现，以控制主窗体的唯一性。
        /// </summary>
        private FormMainSpa()
        {
            // 初始化主窗体中的控件，载入默认模块到窗体的PnlContent控件中。
            InitializeComponent();

            lblName.Text = "当前用户："+S_Admin.Name;        
            Main.Default newControl = Main.Default.Instance;    
            //ManageBilling.BillList newControl = ManageBilling.BillList.Instance;
            PnlContent.Controls.Add(newControl);
            //PnlContent.Controls.Add(ManageBilling.BillList.Instance);
            //FormMainSpa.LoadNewControl(ManageBilling.BillList.Instance);
            adminBll=new AdminLogic();

            SetMenu();

            pctLogo.ImageLocation = "Img/SpaLogo38.jpg";
            lblSysName.Text = SystemInfo.SysName;
            lblVersionKey.Text = SystemInfo.VersionKey;
            //MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            //WindowState = FormWindowState.Maximized;
            //this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }

        public void SetMenu()
        {
            var adminPowerMapps = adminBll.GetAdminPowerMapps(" AdminId = '" + S_Admin.Id + "'").ToList();
            SysPowerManager.AdminPowerList = adminPowerMapps;
            menuBill.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看开单信息);
            menuMember.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看客户列表信息);
            menuService.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看项目信息);
            menuProduct.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看商品信息);
            menuProvider.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看供应商信息);
            menuAdmin.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看员工信息);
            if (S_Admin.Name == "超级管理员") menuAdmin.Enabled = true;
            menuSet.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看设置);
            menuReport.Enabled = SysPowerManager.IsHavePower(SysPowerManager.查看报表);
            //this.w.Visible = SysPowerManager.IsHavePower(SysPowerManager.查看收支);
            //this.y.Visible = SysPowerManager.IsHavePower(SysPowerManager.短信功能);

        }

        /// <summary>
        /// 用户单击窗体左侧“导航栏”中按钮时的事件处理方法。
        /// </summary>
        private void NavBar_ImageButtonClick(object sender, string targetModule)
        {
            switch (targetModule)
            {
                case "Main": // “欢迎使用”功能模块
                    //FormMain.LoadNewControl(Main.Default.Instance);
                    break;

                case "ManageEmployee": // “员工信息管理”功能模块
                    //FormMain.LoadNewControl(ManageEmployee.Default.Instance);
                    break;

                case "ManageDepartment": // “部门管理”功能模块
                    //FormMain.LoadNewControl(ManageDepartment.Default.Instance);
                    break;

            }
        }

        /// <summary>
        /// 用户单击窗体左侧“导航栏”中“退出系统”导航条时的事件处理方法。
        /// </summary>
        private void NavBar_QuitSystemClick(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 载入一个新的功能模块控件，并将其显示在窗体中部模块区域。
        /// </summary>
        public static void LoadNewControl(Control control)
        {
            FormMainSpa.Instance.PnlContent.Controls.Clear();
            FormMainSpa.Instance.PnlContent.Controls.Add(control);
        }

        private void PicCompanyLogo_Click(object sender, EventArgs e)
        {

        }

        private void PnlHeader_Paint(object sender, PaintEventArgs e)
        {
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageMember.MemberList.Instance);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(Main.Default.Instance);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageAdmin.AdminList.Instance);
        }

        private void PnlContent_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pictureBox11_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageBilling.BillList.Instance);
        }

        private void pictureBox10_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageService.ServiceList.Instance);
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            //FormMainSpa.LoadNewControl(ManageSet.SetList.Instance);
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageMoney.MoneyList.Instance);
        }

        private void pictureBox2_Click_1(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageAdmin.AdminList.Instance);
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageProvider.ProviderList.Instance);
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageProduct.ProductList.Instance);
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Close();
        }

        private void mmHome_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(Main.Default.Instance);
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (WindowState == FormWindowState.Maximized) WindowState = FormWindowState.Normal;
            else WindowState = FormWindowState.Maximized;
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuMain_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(Main.Default.Instance);
        }

        private void label3_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageBilling.BillList.Instance);
        }

        private void menuBill_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageBilling.BillList.Instance);
        }

        private void menuMember_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageMember.MemberList.Instance);
        }

        private void label1_Click(object sender, EventArgs e)
        {
                        FormMainSpa.LoadNewControl(ManageService.ServiceList.Instance);
        }

        private void menuService_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageService.ServiceList.Instance);
        }

        private void menuProduct_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageProduct.ProductList.Instance);
        }

        private void menuProvider_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageProvider.ProviderList.Instance);
        }

        private void menuAdmin_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageAdmin.AdminList.Instance);
        }

        private void menuMoney_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageMoney.MoneyList.Instance);
        }

        private void menuSet_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageSet.SetList.Instance);
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {

        }

        private void menuExt_Click(object sender, EventArgs e)
        {
            this.Hide();
            var frmLogin = new FormLogin();
            frmLogin.ShowDialog();
            if (frmLogin.DialogResult != DialogResult.OK) Close();
            else
            {
                this.Show();
                lblName.Text = "当前用户：" + S_Admin.Name; 
            }
            SetMenu();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void PnlHeader_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void PnlHeader_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void pctTool_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void pctMax_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Normal)
            {
                MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                WindowState = FormWindowState.Maximized;
                
            }
            else WindowState = FormWindowState.Normal;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }

        private void menuReport_Click(object sender, EventArgs e)
        {
            FormMainSpa.LoadNewControl(ManageReport.ReportList.Instance);
        }

        private void pctClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PnlHeader_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlHeader_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }

        private void PnlHeader_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }
    }
}
